const baseURL = "https://xzw.zone:8082"; //线上服务器
// const baseURL = "http://127.0.0.1:8000";   //本地服务器

const publicFun = require('public');
const Authorization = 'z+0hp256#x+g9-x+m#r8d&u5tajam'

const http = {
    Request(options) {
        return new Promise((resolve, reject) => {
            if (options.loading) { //默认转圈
                publicFun.Loading(1);
            }
            if (!options.header) {
                options.header = 'x-www-form-urlencoded';
            }
            wx.request({
                url: baseURL + '/dreamApi/' + options.url || '',
                data: options.data || {},
                method: options.method || 'POST',
                header: {
                    'content-type': options.header,
                    'Authorization': Authorization
                },
                responseType: options.responseType || "",
                timeout: 15000,
                success(res) {
                    if (options.loading) {
                        publicFun.LoadingOff();
                    }
                    if (res.statusCode === 200) {
                        if (res.data.code === 200) {
                            resolve(res.data);
                        } else {
                            publicFun.Toast('请求失败！');
                        };
                    } else {
                        publicFun.Toast('服务错误！');
                    };
                },
                fail(res) {
                    publicFun.Toast("网络开小差了！");
                    reject(res);
                }
            })
        })
    },
    //下载文件
    DownloadFile(path) {
        return new Promise((resolve, reject) => {
            wx.downloadFile({
                url: baseURL + path,
                header: {
                    'Content-Type': 'x-www-form-urlencoded',
                    'Authorization': Authorization
                },
                success(res) {
                    resolve(res.tempFilePath)
                }
            })
        })
    },
}

module.exports = http;